package com.uchengwang.ui.self.banner;

import com.uchengwang.R;
import com.uchengwang.ui.cm_ui.ucity.cityBean.BannerBean;
import com.uchengwang.ui.self.banner.holder.CBViewHolderCreator;
import com.uchengwang.ui.self.banner.holder.FileImageHolderView;
import com.uchengwang.ui.self.banner.holder.LocalImageHolderView;
import com.uchengwang.ui.self.banner.holder.NetworkImageHolderView;
import com.uchengwang.ui.self.banner.holder.NetworkImageTitleHolderView;
import com.uchengwang.ui.self.banner.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by U城网 on 2017/3/14.
 * 简单初始化ConvenientBanner类
 */

public class BannerManager {
    private static BannerManager manager;

    private BannerManager(){}

    public static BannerManager getBannerManager() {
        if (manager == null){
            manager = new BannerManager();
        }
        return manager;
    }

    /**
     * @param banner ConvenientBanner 对象
     * @param bannerItems res资源图片id
     * @param listener Item点击事件
     * @param pointIsVisible 指示器是否可见
     * @param isCanAutoScroll 是否自动滚动
     * */
    public ConvenientBanner setLocalBannerData(ConvenientBanner banner, List<Integer> bannerItems, boolean pointIsVisible,
                                               boolean isCanAutoScroll, OnItemClickListener listener){
        banner.setPages(new CBViewHolderCreator<LocalImageHolderView>() {
            @Override
            public LocalImageHolderView createHolder() {
                return new LocalImageHolderView();
            }
        }, bannerItems);    //设置需要切换的View地址

        setBanner(banner, pointIsVisible, isCanAutoScroll, listener);
        return banner;
    }

    public ConvenientBanner setLocalBannerData(ConvenientBanner banner, Integer resId, boolean pointIsVisible,
                                               boolean isCanAutoScroll, OnItemClickListener listener){
        List<Integer> bannerItems = new ArrayList<>();
        bannerItems.add(resId);

        banner.setPages(new CBViewHolderCreator<LocalImageHolderView>() {
            @Override
            public LocalImageHolderView createHolder() {
                return new LocalImageHolderView();
            }
        }, bannerItems);    //设置需要切换的View地址

        setBanner(banner, pointIsVisible, isCanAutoScroll, listener);
        return banner;
    }

    /**
     * @param banner ConvenientBanner 对象
     * @param bannerItems 服务器返回的banner资源
     * @param listener Item点击事件
     * @param pointIsVisible 指示器是否可见
     * @param isCanAutoScroll 是否自动滚动
     * */
    public ConvenientBanner setNetBannerTitle(ConvenientBanner banner, List<BannerBean> bannerItems, boolean pointIsVisible,
                                         boolean isCanAutoScroll, OnItemClickListener listener){
        banner.setPages(new CBViewHolderCreator<NetworkImageTitleHolderView>() {
            @Override
            public NetworkImageTitleHolderView createHolder() {
                return new NetworkImageTitleHolderView();
            }
        }, bannerItems);    //设置需要切换的View地址

        setBanner(banner, pointIsVisible, isCanAutoScroll, listener, ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT);
        return banner;
    }

    /**
     * @param banner ConvenientBanner 对象
     * @param items 网络图片地址
     * @param listener item点击事件
     * @param isNeed 是否需要拼接
     * @param pointIsVisible 指示器是否可见
     * @param isCanAutoScroll 是否自动滚动
     * @param type 1头像  2享说 3U城内容,社团纳新,社团/校吧内的贴子内的图片;
     * */
    public ConvenientBanner setNetBannerData(ConvenientBanner banner, final int type, List<String> items, final boolean isNeed, boolean pointIsVisible,
                                             boolean isCanAutoScroll, OnItemClickListener listener){
        banner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView(isNeed, type);
            }
        }, items);    //设置需要切换的View地址
        setBanner(banner, pointIsVisible, isCanAutoScroll, listener);
        return banner;
    }

    /**
     * @param banner ConvenientBanner 对象
     * @param items 本地图片地址
     * @param listener item点击事件
     * @param pointIsVisible 指示器是否可见
     * @param isCanAutoScroll 是否自动滚动
     * */
    public ConvenientBanner setFileBannerData(ConvenientBanner banner, List<String> items, boolean pointIsVisible,
                                              boolean isCanAutoScroll, OnItemClickListener listener){
        banner.setPages(new CBViewHolderCreator<FileImageHolderView>() {
            @Override
            public FileImageHolderView createHolder() {
                return new FileImageHolderView();
            }
        }, items);    //设置需要切换的View地址

        setBanner(banner, pointIsVisible, isCanAutoScroll, listener);
        return banner;
    }

    private void setBanner(ConvenientBanner banner, boolean pointIsVisible, boolean isCanAutoScroll, OnItemClickListener listener){
        banner.setPointViewVisible(pointIsVisible)    //设置指示器是否可见
                .setPageIndicator(new int[]{R.drawable.indicator_normal, R.drawable.indicator_focus})   //设置指示器圆点
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL) //设置指示器位置（左、中、右）
                .setOnItemClickListener(listener); //设置点击监听事件

        if (isCanAutoScroll){
            banner.startTurning(5000);// 设置自动切换（同时设置了切换时间间隔）
        }else {
            banner.stopTurning();
        }


    }

    private void setBanner(ConvenientBanner banner, boolean pointIsVisible, boolean isCanAutoScroll, OnItemClickListener listener, ConvenientBanner.PageIndicatorAlign align){
        banner.setPointViewVisible(pointIsVisible)    //设置指示器是否可见
                .setPageIndicator(new int[]{R.drawable.indicator_normal, R.drawable.indicator_focus})   //设置指示器圆点
                .setPageIndicatorAlign(align) //设置指示器位置（左、中、右）
                .setOnItemClickListener(listener); //设置点击监听事件

        if (isCanAutoScroll){
            banner.startTurning(5000);// 设置自动切换（同时设置了切换时间间隔）
        }else {
            banner.stopTurning();
        }


    }


}
